package js.lee.jvli.kit;

import java.io.File;
import java.io.InputStream;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XmlKit {
    @SuppressWarnings("unchecked")
    public static <T> T toBean(String xml, Class<T> objClass) {
        XStream xstream = new XStream(new DomDriver());
        xstream.processAnnotations(objClass);
        T obj = (T) xstream.fromXML(xml);
        return obj;
    }

    @SuppressWarnings("unchecked")
    public static <T> T toBean(File xml, Class<T> objClass) {
        XStream xstream = new XStream(new DomDriver());
        xstream.processAnnotations(objClass);
        T obj = (T) xstream.fromXML(xml);
        return obj;
    }

    @SuppressWarnings("unchecked")
    public static <T> T toBean(InputStream xml, Class<T> objClass) {
        XStream xstream = new XStream(new DomDriver());
        xstream.processAnnotations(objClass);
        T obj = (T) xstream.fromXML(xml);
        return obj;
    }

    public static String toXml(Object obj) {
        XStream xstream = new XStream();
        xstream.processAnnotations(obj.getClass());
        return xstream.toXML(obj);
    }
}
